﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;


namespace OGS.Utility
{
    public class SettingsData
    {

        private bool isLoadComplete = false;
        private string settingsFilePath = AppDomain.CurrentDomain.BaseDirectory + "Application.config";

        public bool IsLoadComplete
        {
            get
            {
                return isLoadComplete;
            }
        }

        /// <summary>
        /// get Data XML
        /// </summary>
        protected XmlDocument Data
        {
            get;
            set;
        }

        /// <summary>
        /// Get or set filepath
        /// </summary>
        public string FilePath
        {
            get
            {
                return settingsFilePath;
            }
            set
            {
                settingsFilePath = value;
            }
        }

        /// <summary>
        /// Load Settings Xml
        /// </summary>
        public void Load()
        {
            if (File.Exists(this.FilePath))
            {
                loadSettings();
            }
        }

        /// <summary>
        /// Select specific node by key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private XmlNode selectNode(string key)
        {
            string xPath = string.Format("//appSettings/add[@key='{0}']/@value", key);
            return this.Data.SelectSingleNode(xPath);
        }

        /// <summary>
        /// get text as string data type
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string Get(string key)
        {
            return XMLHelper.GetInnerText(selectNode(key));
        }

        /// <summary>
        /// get text as int data type
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public int GetInt(string key)
        {
            return XMLHelper.GetInnerTextInt(selectNode(key));
        }

        /// <summary>
        /// get text as long data type
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public long GetLong(string key)
        {

            return XMLHelper.GetInnerTextLong(selectNode(key));
        }

        /// <summary>
        /// get text as bool data type
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool GetBool(string key)
        {
            return XMLHelper.GetInnerText(selectNode(key)).ToLower().Equals("true");
        }
        
        /// <summary>
        /// for mail content
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetXML(string key)
        {
            
            string xPath = string.Format("//appSettings/add[@key='{0}']", key);
            string xmlText = XMLHelper.GetInnerXml(this.Data.SelectSingleNode(xPath)).Trim();
            if (xmlText.StartsWith("<![CDATA[") && xmlText.EndsWith("]]>"))
            {
                xmlText = xmlText.Replace("<![CDATA[", "").Replace("]]>", "");
            }
            return xmlText;
        }

        /// <summary>
        /// Load Settings
        /// </summary>
        private void loadSettings()
        {
            this.Data = null;
            this.Data = new XmlDocument();
            try
            {
                string data = System.IO.File.ReadAllText(this.FilePath);
                this.Data.LoadXml(data);                
                isLoadComplete = true;
               
            }
            catch (Exception ex)
            {
                isLoadComplete = false;
            }
        }
    }
}
